package com.lelouch.service;

import com.google.common.collect.Lists;
import com.lelouch.bean.*;
import com.lelouch.beans.LogType;
import com.lelouch.common.RequestHolder;
import com.lelouch.dao.SysLogMapper;
import com.lelouch.dao.SysRoleAclMapper;
import com.lelouch.dao.SysRoleUserMapper;
import com.lelouch.dao.SysUserMapper;
import com.lelouch.util.IpUtil;
import com.lelouch.util.JsonMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lcy
 * @create 2018-06-23 15:31
 */
@Service
public class SysRoleUserService {

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;
    @Autowired
    private SysLogMapper sysLogMapper;

    public List<SysUser> getUserListByRoleId(int roleId) {

        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleidList(Lists.newArrayList(roleId));
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        List<SysUser> sysUsers = sysUserMapper.getUserListByIdList(userIdList);
        if (CollectionUtils.isEmpty(sysUsers)) {
            return Lists.newArrayList();
        }
        return sysUsers;
    }

    public void changeRoleUsers(int roleId, List<Integer> userIdList) {
        List<Integer> oldUserIdList = sysRoleUserMapper.getUserIdListByRoleidList(Lists.newArrayList(roleId));//todo
        if (userIdList.size() == oldUserIdList.size()) {
            Set<Integer> newIdSet = userIdList.stream().collect(Collectors.toSet());
            Set<Integer> oldIdSet = oldUserIdList.stream().collect(Collectors.toSet());
            newIdSet.removeAll(oldIdSet);
            if (CollectionUtils.isEmpty(newIdSet)) {
                return;
            }
        }
        updateRoleUsers(roleId, userIdList);
        saveRoleUserLog(roleId,oldUserIdList,userIdList);
    }


    @Transactional
    public void updateRoleUsers(int roleId, List<Integer> userIdList) {
        sysRoleUserMapper.deleteByRoleId(roleId);

        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<SysRoleUser> roleUserList = Lists.newArrayList();
        for (Integer userId : userIdList) {
            SysRoleUser roleUser = SysRoleUser.builder().roleId(roleId).userId(userId).operator(RequestHolder.getUser().getUsername())
                    .operateIp(IpUtil.getRemoteIp(RequestHolder.getRequest())).operateTime(new Date()).build();
            roleUserList.add(roleUser);
        }
        sysRoleUserMapper.batchInsert(roleUserList);

    }


    public List<SysUser> getUserListByAclId(Integer aclId) {
        List<Integer> roleIdList = sysRoleAclMapper.getRoleListByAclId(aclId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleidList(roleIdList);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        return sysUserMapper.getUserListByIdList(userIdList);
    }

    private void saveRoleUserLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLog = new SysLogWithBLOBs();
        sysLog.setType(LogType.TYPE_ROLE_USER);
        sysLog.setTargetId(roleId);
        sysLog.setOldValue(before == null ? "" : JsonMapper.obj2String(before));
        sysLog.setNewCalue(after == null ? "" : JsonMapper.obj2String(after));
        sysLog.setOperator(RequestHolder.getUser().getUsername());
        sysLog.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        sysLog.setOperateTime(new Date());
        sysLog.setStatus(1);
        sysLogMapper.insertSelective(sysLog);
    }
}
